//
//  CYUserCarDropCarVC.m
//  CreditAddressBook
//
//  Created by LE on 15/12/17.
//  Copyright © 2015年 Lee. All rights reserved.
//

#import "CYUserCarDropCarVC.h"
#import "CYCarInfoCell.h"
#import "UITableViewCell+Category.h"
#import "CYTool.h"
#import "CYLoginHttpTool.h"
#import "CYCompanyUserTool.h"
#import "MBProgressHUD+MJ.h"
#import "CYDiscoveryHttpTool.h"
#import "UIImageView+WebCache.h"
#import "CYCarRentalDetailTableVC.h"
#import "CYJumpsTool.h"

@interface CYUserCarDropCarVC ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, weak) UITableView *tableview;
@property(nonatomic, strong) NSArray *carRentals;

@property(nonatomic, weak) UILabel *tipLabel;
@end

@implementation CYUserCarDropCarVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTable];
    [CYTool setExtraCellLineHiddenWithTableView:self.tableview];
    [self loadData];
    [self setupNoti];
}

- (void)setupTable {
    UITableView *table = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    table.delegate = self;
    table.dataSource = self;
    table.contentInset = UIEdgeInsetsMake(0, 0, 110, 0);
    [self.view addSubview:table];
    self.tableview = table;
    //无数据提示view
    UILabel *label = [[UILabel alloc] init];
    label.text = @"您还没有发布任何租车信息！";
    label.textColor = [UIColor grayColor];
    label.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:label];
    [label autoCenterInSuperview];
    self.tipLabel = label;
    label.hidden = YES;
}

- (void)loadData {
    NSDictionary *param = nil;
    if (self.userType == CYUserTypePerson) {
        CYUser *user = [CYLoginHttpTool userData].user;
        if (user) {
            param = @{@"userId": user.userId, @"token": user.token, @"type": @(self.userType), @"page": @"0"};
        } else {
            [MBProgressHUD showError:@"请登录后再操作"];
        }
    } else if (self.userType == CYUserTypeCompany) {
        CYCompanyUserModel *user = [CYCompanyUserTool companyUser];
        if (user) {
            param = @{@"userId": user.carCompanyUserId, @"token": user.token, @"type": @(self.userType), @"page": @"0", @"companyId": user.companyId};
        } else {
            [MBProgressHUD showError:@"请登录后再操作"];
        }
    }
    if (param) {
        [MBProgressHUD showMessage:@"加载中" toView:self.view];
        [CYDiscoveryHttpTool carRentalListWithParam:param userType:CYCarStatusTypeDroped success:^(NSArray *carRentalList) {
            [MBProgressHUD hideHUDForView:self.view];
            if (carRentalList.count) {
                self.carRentals = carRentalList;
                [self.tableview reloadData];
            } else {
                self.tipLabel.hidden = NO;
            }
        } failure:^(NSString *message, NSError *error) {
            [MBProgressHUD hideHUDForView:self.view];
            if (message) {
                [MBProgressHUD showError:message];
            } else if (error) {
                [MBProgressHUD showError:@"服务器连接错误"];
            }
        }];
    }
}

#pragma mark - UITableViewDelegate UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.carRentals.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CYCarRental *car = self.carRentals[indexPath.row];
    
    CYCarInfoCell *cell = [CYCarInfoCell cellWithTableView:tableView];
    cell.statusView.hidden = YES;
    cell.mark.hidden = YES;
    if (car.photo && !car.photo.isEmpty) {
        [cell.carImgeView sd_setImageWithURL:CYURLWithUserService(car.photo) placeholderImage:[UIImage imageNamed:@"putao_icon_quick_replace_s"]];
    }
    cell.carName.text = car.name;
    cell.plateNumber.text = car.phone;
    cell.carEngineNumber.text = car.plateNumber;
    cell.carFrameNumber.text = car.carRentalDate;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    CYCarRental *car = self.carRentals[indexPath.row];
    CYCarRentalDetailTableVC *detail = [CYCarRentalDetailTableVC le_loadFromStoryBoard:[CYJumpsTool mainSB]];
    detail.showDropCarItem = NO;
    detail.carRental = car;
    detail.userType = self.userType;
    [self.navigationController pushViewController:detail animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

#pragma mark - 通知
- (void)setupNoti {
    [CYNotificationCenter addObserver:self selector:@selector(reloadCarLeaseData:) name:nCYReloadDataOfCarLeaseAndDrop object:nil];
}

- (void)reloadCarLeaseData:(NSNotification *)noti {
    [self loadData];
}
@end
